package game;

import game.Game;
import game.GameEvent;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class GameClient {
	
	Game game;
	String request;
	String id;
	
	public GameClient(Game g, String request) {
		
		this.game = g;	
		this.request = request;
		String[] req  = this.request.split("\n");
		if(req[0].contains("connect")) {
			this.id = java.util.UUID.randomUUID().toString();
		} else {
			this.id = req[0];
		}
		
	}
	
	public String getId() {
		
		return this.id;
	} 
	
	public String work() {
		 
		 String[] req  = this.request.split("\n");
		 
		 if(req[0].contains("connect")) {
			
			 this.game.createEvent(this.id, "connect", "");
			 
			 return new GameEvent(this.id, this.id, "connect", "OK").toString();
			 
		 } else {
			 if(!req[1].contains("wait"))
				 return this.game.createEvent(this.id, req[1], req[2]).toString();
		 }
		 		 				 		 
		 
		 GameEvent evt = null;
		 while((evt = this.game.getEvent(this)) == null)
			try {
				Thread.sleep(100);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		
		this.game.removeClient(this); 
		return evt.toString(); 			 
		
	}

}
